package com.example.common;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Message<T> implements Serializable {

    private static final long serialVersionUID = -8734072576335328905L;

    private int code;
    private String msg;
    private T data;

    private Message(RstStatus status) {
        this(status, null, null);
    }

    private Message(RstStatus status, T data) {
        this(status, null, data);
    }

    private Message(RstStatus status, String msg, T data) {
        if (status != null) {
            this.code = status.getCode();
            this.msg = status.getDesc();
        }
        if (StringUtils.isNotBlank(msg)) {
            this.msg = msg;
        }
        if (data != null) {
            this.data = data;
        }
    }

    private Message(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Message<T> ok() {
        return new Message<>(RstStatus.ok);
    }

    public static <T> Message<T> ok(String msg, T data) {
        return new Message<>(RstStatus.ok, msg, data);
    }

    public static <T> Message<T> ok(T data) {
        return new Message<>(RstStatus.ok, data);
    }

    /*
     * !!error方法只在ExceptionHandler中使用，其它位置请直接抛出异常
     */
    public static <T> Message<T> error(RstStatus status) {
        return new Message<>(status);
    }

    public static <T> Message<T> error(RstStatus status, String msg) {
        return new Message<>(status, msg, null);
    }

    public static <T> Message<T> error(int code, String msg) {
        return new Message<>(code, msg, null);
    }

    public static <T> Message<T> error(String msg) {
        return new Message<>(500, msg, null);
    }


    public static <T> Message<T> error(int code, String msg, T data) {
        return new Message<>(code, msg, data);
    }

    public static <T> Message<T> error(RstStatus status, String msg, T data) {
        return new Message<>(status, msg, data);
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Object getData() {
        return data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static Message getMessageByResult(ResultWithCode resultWithCode) {
        if (resultWithCode.getCode() == 1) {
            //成功
            return Message.ok("操作成功", resultWithCode.getData());
        } else {
            RstStatus status = RstStatus.getStatusByCode(resultWithCode.getCode());
            if (status != null) {
                return error(status, resultWithCode.getMessage());
            }
            return error(RstStatus.serviceError, "未知服务异常");
        }
    }

    public static <T> Message ok(List<T> list, long totalCount, int totalPage, int pageSize, int pageNum) {
        Map<String, Object> data = new HashMap();
        data.put("totalCount", totalCount);
        data.put("totalPage", totalPage);
        data.put("pageSize", pageSize);
        data.put("pageNum", pageNum);
        if (list != null) {
            data.put("list", list);
        }
        return Message.ok(data);
    }
}
